package com.software.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.software.base.ResultInfo;
import com.software.bean.OrderBean;
import com.software.dto.*;
import com.software.service.OrderService;
import com.software.utils.AssertUtil;
import com.software.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-04-04
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * @description:点击支付后调用的方法——--添加订单
     * @author: wyj
     * @date: 2024/4/4 19:40
     * @param:
     * @return:
     */
    @PostMapping("/front/postOrder")
    @ResponseBody
    public ResultInfo postOrder(@RequestBody AddOrderDto addOrderDto) {
        System.out.println(addOrderDto);
        List<Integer> orderBeans = orderService.addOrder(addOrderDto);
        return ResultInfo.success(orderBeans);
    }

    /**
     * @description:获取用户的交易订单列表
     * @author: wyj
     * @date: 2024/4/5 1:01
     * @param:
     * @return:
     */
    @GetMapping("/front/getOrderListByCustomerId")
    @ResponseBody
    public ResultInfo getOrderListByCustomerId(@RequestParam("customerId") Integer customerId) {
        List<OrderBean> list = orderService.getOrderList(customerId);
        return ResultInfo.success(list);
    }


    /**
     * @description:查所有卖出的商品
     * @author: wyj
     * @date: 2024/4/5 0:05
     * @param:
     * @return:
     */
    @GetMapping("/front/getProductForDeliveryList")
    @ResponseBody
    public ResultInfo getProductForDeliveryList(@RequestParam("customerId") Integer customerId) {
        List<OrderBean> list = orderService.getProductForDeliveryList(customerId);
        return ResultInfo.success(list);
    }

    /**
     * @description:发货
     * @author: wyj
     * @date: 2024/4/5 12:59
     * @param:
     * @return:
     */
    @PostMapping("/front/postDeliveryNo")
    @ResponseBody
    public ResultInfo postDeliveryNo(@RequestBody AddDeliveryNoDto addDeliveryNoDto) {

        Integer result = orderService.addDelivery(addDeliveryNoDto);
        return ResultInfo.success(result);
    }

    /**
     * @description:分页拉取订单列表
     * @author: wyj
     * @date: 2024/4/5 12:59
     * @param:
     * @return:
     */
    @GetMapping("/api/getOrderList")
    @ResponseBody
    public ResultInfo getOrderList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        IPage<OrderBean> iPage = orderService.getOrderListForPage(pageNum, pageSize);
        return ResultInfo.success((int) iPage.getTotal(), iPage.getRecords());
    }


    /**
     * @description:获取支付页面的订单信息
     * @author: wyj
     * @date: 2024/4/10 20:48
     * @param:
     * @return:
     */
    @PostMapping("/front/getOrderInfoByOrderId")
    @ResponseBody
    public ResultInfo getOrderInfoByOrderId(@RequestBody OrderInfoByOrderIdsDto orderInfoByOrderIdsDto) {
        System.err.println(orderInfoByOrderIdsDto);
        System.out.println(123456);
        OrderVo orderVo = orderService.getOrderInfoByOrderId(orderInfoByOrderIdsDto);
        return ResultInfo.success(orderVo);
    }

    /**
     * @description:支付功能
     * @author: wyj
     * @date: 2024/4/5 12:26
     * @param:
     * @return:
     */
    @PostMapping("/front/postOrderToPay")
    @ResponseBody
    public ResultInfo postOrderToPay(@RequestBody OrderPayDto orderPayDto) {
        System.out.println(orderPayDto);
        Integer result = orderService.postOrderToPay(orderPayDto);
        AssertUtil.isTrue(result != 1, "支付失败");
        return ResultInfo.success();
    }

    /**
     * @description:查订单详情用于订单详情页面
     * @author: wyj
     * @date: 2024/4/5 13:47
     * @param:
     * @return:
     */
    @GetMapping("/front/getOrderDetailByOid")
    @ResponseBody
    public ResultInfo getOrderDetailByOid(@RequestParam Integer oid) {
        OrderBean orderBean = orderService.getOrderDetailByOid(oid);
        return ResultInfo.success(orderBean);
    }

    /**
     * @description:退款功能实现
     * @author: wyj
     * @date: 2024/4/5 14:56
     * @param:
     * @return:
     */
    @PutMapping("/front/postOrderToRefund/{oid}")
    @ResponseBody
    public ResultInfo putOrderToRefund(@PathVariable Integer oid) {
        Integer result = orderService.putOrderToRefund(oid);
        AssertUtil.isTrue(result!= 1 , "退款失败");
        return ResultInfo.success();
    }

    /**
     * @description:收货功能实现
     * @author: wyj
     * @date: 2024/4/5 15:52
     * @param:
     * @return:
     */
    @PutMapping("/front/putOrderToReceive/{oid}")
    @ResponseBody
    public ResultInfo putOrderToReceive(@PathVariable Integer oid) {
        Integer result = orderService.putOrderToReceive(oid);
        return ResultInfo.success();
    }

    /**
     * @description:后台点击详情按钮获取订单详细信息
     * @author: wyj
     * @date: 2024/4/5 21:09
     * @param:
     * @return:
     */
    @GetMapping("/api/getOrderDetail/{oid}")
    @ResponseBody
    public ResultInfo getOrderDetail(@PathVariable Integer oid) {
        OrderBean orderBean = orderService.getOrderDetail(oid);
        return ResultInfo.success(orderBean);
    }

    /**
     * @description:订单状态饼图
     * @author: wyj
     * @date: 2024/4/9 16:30
     * @param:
     * @return:
     */
    @GetMapping("/api/orderCharts")
    @ResponseBody
    public ResultInfo orderCharts() {
        List<Map<String, Object>> mapList = orderService.orderCharts();
        return ResultInfo.success(mapList);
    }

    /**
     * @description:卖家取消订单
     * @author: wyj
     * @date: 2024/4/9 17:22
     * @param:
     * @return:
     */
    @PutMapping("/front/cancelOrder/{oid}")
    @ResponseBody
    public ResultInfo cancelOrder(@PathVariable Integer oid) {
        Integer result = orderService.cancelOrder(oid);
        AssertUtil.isTrue(result != 1, "取消订单失败");
        return ResultInfo.success();
    }

    /**
     * @description:卖家取消支付
     * @author: wyj
     * @date: 2024/4/10 22:42
     * @param:
     * @return:
     */
    @PostMapping("/front/cancelPay")
    @ResponseBody
    public ResultInfo cancelPay(@RequestBody CancelOrderPayDto cancelOrderPayDto) {
        Integer result = orderService.cancelPay(cancelOrderPayDto);
        AssertUtil.isTrue(result != 1, "取消支付失败");
        return ResultInfo.success();
    }
}